﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmKhoChuyenDoi : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private ChuyenKhoBOCollection chuyenKhoBOCollection = null;

        #endregion variant

        //=====================================
        #region form
        public FrmKhoChuyenDoi()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmKhoChuyenDoi_Load);
            this.Shown += new EventHandler(FrmKhoChuyenDoi_Shown);
            this.FormClosing += new FormClosingEventHandler(FrmKhoChuyenDoi_FormClosing);

            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnTimKiem.Click += new EventHandler(btnTimKiem_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.grdViewTonKho.RowCellStyle += new RowCellStyleEventHandler(grdView_RowCellStyle);

            this.grdViewTonKho.ShowingEditor += new CancelEventHandler(grdView_ShowingEditor);
        }

        private void FrmKhoChuyenDoi_Shown(object sender, EventArgs e)
        {
            this.grdViewTonKho.Focus();
        }

        private void FrmKhoChuyenDoi_Load(object sender, EventArgs e)
        {
            RefreshData("");

            Utility.InitControl(this.reLookUpMatHangID);
            Utility.InitControl(this.reLookUpDonViTinh);
            Utility.InitControl(this.reLookUpDanhMucKhoID);
        }

        private void FrmKhoChuyenDoi_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData("");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {

            string strWhere = "";

            if (checkNgay.Checked)
            {
                strWhere = strWhere + " convert(nvarchar,DATC,111) >= '" + dateEditTungay.DateTime.ToString("yyyy/MM/dd") + "' and convert(nvarchar,DATC,111) <= '" + dateEditDenngay.DateTime.ToString("yyyy/MM/dd") + "'";
            }

            RefreshData(strWhere);
        }

        #endregion button

        //=====================================
        #region grid

        private void grdView_ShowingEditor(object sender, CancelEventArgs e)
        {
            GridView grdView = (GridView)sender;

            bool del = StaticUtil.GetBool(grdView.GetFocusedRowCellValue("DELE"));
            string fieldName = grdView.FocusedColumn.FieldName;

            if (del && fieldName != "DELE")
                e.Cancel = true;
            else
                if (fieldName == "DELE" && grdView.FocusedRowHandle == GridControl.NewItemRowHandle)
                    e.Cancel = true;
        }

        private void grdView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool del = StaticUtil.GetBool(this.grdViewTonKho.GetRowCellValue(e.RowHandle, "DELE"));

            if (del)
            {
                e.Appearance.Font = MyStyle.DELETE_FONT;
                e.Appearance.ForeColor = MyStyle.DELETE_COLOR;
            }
        }
        #endregion grid

        //=====================================
        #region function
        private void RefreshData(string strwhere)
        {

            #region

            DanhMucBOList donViTinhBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DonViTinh);
            Utility.SetData(this.reLookUpDonViTinh, donViTinhBOList, donViTinhBOList.Count);

            DanhMucBOList danhMucKhoBOList = DanhMucBOList.GetDanhMucBOList(LoaiDanhMuc.DanhMucKho);
            Utility.SetData(this.reLookUpDanhMucKhoID, danhMucKhoBOList, danhMucKhoBOList.Count);

            MatHangBOList matHangBOList = MatHangBOList.GetMatHangBOList();
            Utility.SetData(this.reLookUpMatHangID, matHangBOList, matHangBOList.Count);

            dateEditTungay.DateTime = dateEditDenngay.DateTime = DateTime.Now;
            #endregion BOList

            this.chuyenKhoBOCollection = ChuyenKhoBOCollection.GetChuyenKhoBOCollection(strwhere, "ChuyenKhoID", "DESC");

            this.bdSourceTonKho.DataSource = this.chuyenKhoBOCollection.Clone();
        }

        #endregion function

    }
}